package com.snut.tomorrowNews.controller.back;

import com.snut.tomorrowNews.common.CommonResult;
import com.snut.tomorrowNews.model.Admin;
import com.snut.tomorrowNews.model.Role;
import com.snut.tomorrowNews.service.back.AdminService;
import com.snut.tomorrowNews.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping(path = "/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 查询管理员
     *
     * @return
     */
    @RequestMapping(path = "/findAdmin")
    public CommonResult findAdmin(Integer id) {
        CommonResult commonResult = null;
        try {
            Admin admin = adminService.findAdmin(id);
            commonResult = new CommonResult(200, "查询成功", admin);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败", null);
        }
        return commonResult;
    }
    /**
     * 保存角色
     * @return
     */
   /* @RequestMapping(path = "/save")
    public CommonResult save(Admin admin){
        CommonResult commonResult = null;
        try {
            if(admin.getId()==null){
                adminService.saveAdmin(admin);
            }else{
                adminService.alterAdmin(admin);
            }

            commonResult = new CommonResult(200, "保存成功", null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500, "保存失败",null);
        }
        return commonResult;
    }*/

    /**
     * 查询角色列表
     *
     * @return
     */
    @RequestMapping(path = "/roles")
    public CommonResult roles() {
        CommonResult commonResult = null;
        try {
            List<Role> menus = adminService.getRoleList();
            commonResult = new CommonResult(200, "查询成功", menus);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败", null);
        }
        return commonResult;
    }

    /**
     * 查询管理员列表
     *
     * @return
     */
    @RequestMapping(path = "/admins")
    public CommonResult admins() {
        CommonResult commonResult = null;
        try {
            List<Admin> admins = adminService.getAdminList();
            commonResult = new CommonResult(200, "查询成功", admins);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败", null);
        }

        return commonResult;
    }

    /**
     * 文件上传
     * 接收文件
     * apache组件+springMVC 接收组装的文件
     *
     * @RequestParam("fileName") CommonsMultipartFile file
     */
    @PostMapping(path = "/fileUpload")
    public CommonResult fileUpload(@RequestParam("fileName") CommonsMultipartFile file, HttpSession session) {
        CommonResult commonResult = null;
          /*
            拿到session中的当前登录账号信息 在D:/userImg/账号信息/文件名
           */
        Admin admin = (Admin) session.getAttribute("admin");
        File f0 = new File("D:\\test\\tomorrowNewsFiles\\admin\\" + admin.getAccount());
        if (!f0.exists()) {
            f0.mkdir();
        }
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.getNewFileName(oldFileName);//生成新文件名
        File f = new File(f0, newFileName);//输出路径
        try {
            file.transferTo(f);//将文件输出到指定的目录中
            //保存用户和文件关系
            Admin a = new Admin();
            a.setId(admin.getId());
            a.setNewFileName(newFileName);
            a.setOldFileName(oldFileName);
            adminService.updateAdmin(a);
            commonResult = new CommonResult(200, "上传成功", a);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "上传失败", null);
        }
        return commonResult;
    }

    //修改密码
    @RequestMapping(path = "/updatapas", method = RequestMethod.POST)
    public CommonResult updataPas(@Param("id") Integer id, @Param("newpas") String newpas, @Param("old") String old) {
        CommonResult commonResult = null;
        try {
            boolean m = adminService.updatapas(id, newpas, old);
            if (m) {
                commonResult = new CommonResult(200, "修改成功", null);
            } else {
                commonResult = new CommonResult(201, "密码有误", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            commonResult = new CommonResult(500, "查询失败", null);
        }
        return commonResult;
    }

}
